package com.fx.zmlzml.controller;

import com.fx.zmlzml.common.response.Result;
import com.fx.zmlzml.entity.SysUser;
import com.fx.zmlzml.service.SysService;
import com.github.pagehelper.util.StringUtil;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/sys")
public class SysController {

    @Autowired
    private SysService sysService;

    /**
     * 登录用户
     * @return 登录成功
     */
    @GetMapping("/test")
    public Result<String> test() {
        return Result.success();
    }

    /**
     * 登录用户
     * @return 登录成功
     */
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody SysUser sysUser) {
        if (StringUtil.isEmpty(sysUser.getUsername())) {
            return Result.error("用户名不能为空");
        }
        if (StringUtil.isEmpty(sysUser.getPassword())) {
            return Result.error("密码不能为空");
        }
        return sysService.login(sysUser);
    }

    /**
     * 注册用户
     * @return 注册成功
     */
    @PostMapping("/register")
    public String register() {
        return "注册成功";
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public String logout() {
        return "退出成功";
    }

    /**
     * 强制退出用户
     * @return 强制退出成功
     */
    @PostMapping("/forceLogout")
    public String forceLogout() {
        return "强制退出成功";
    }

    /**
     * 注销用户
     */
    @PostMapping("/logoutAll")
    public String logoutAll() {
        return "注销用户成功";
    }

    /**
     * 用户是否存在
     * @return 用户是否存在
     */
    @GetMapping("/userExists")
    public Result<Boolean> userExists(
            @Parameter(description = "用户名", example = "zml", required = true) @PathVariable String username) {
        if (StringUtil.isEmpty(username)) {
            return Result.error("用户名不能为空");
        }
        return Result.success(sysService.userExists(username));
    }

}
